package com.dwyinyuan.producer;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.stream.Collectors;

public class Huashi6Producer implements Runnable {

    private String name;
    private BlockingQueue<String> blockingQueue;

    public Huashi6Producer(String name, BlockingQueue<String> blockingQueue) {
        this.name = name;
        this.blockingQueue = blockingQueue;
    }


    public static final String Proto = "https://";
    public static final String BasicUrl = "www.huashi6.com";
    static final String ImageUrlCssQuery = ".p-painter-detail .painter-page-body .hot-work-list .hot-work-page .cover-img .img-vec,.img-hor";
    static final String PainterHomePageCssQuery = ".search-container .container .painter .classify-painter .c-painter-item .painter .painter-info";

    @Override
    public void run() {

        try {
            /**
             *
             * 如何找画师id？看下面
             * 首页也可以看到画师ID
             * https://www.huashi6.com/painter/14040
             * **P站画师ID：58434088**;谢谢喜欢~转载标注出处id~ 约稿请联系邮箱:kh3ovo@qq.com
             */
            // 输入画师id
            String painterId = "9366475";//

            StringBuffer buffer = new StringBuffer(Proto + BasicUrl + "/search?searchText=");
            String url = buffer.append(painterId).toString();
            // 1.根据url连接返回document

            Document document = Jsoup.connect(url).get();
            // 2.根据层级元素查找画师的主页url并跳转
            String painterHomePageUrl = this.getPainterHomePageURL(document, PainterHomePageCssQuery, "a", "href");

            String painterHomeUrl = Proto + BasicUrl + painterHomePageUrl;
            // 根据画师主页 连接返回document
            Document documentPainterHomePage = Jsoup.connect(painterHomeUrl).get();

            // 3.获取图片信息，包括图片链接和图片名称
            List<String> imgURLList = this.getInfoList(documentPainterHomePage, ImageUrlCssQuery, "img", "src");
            for (String pic : imgURLList) {
                blockingQueue.put(pic);
                System.out.println(name + " put： " + pic);
            }
            //消费者根据""退出while循环
            blockingQueue.put("");
            System.out.println("-----------------------------------");
            System.out.println(name + " is over");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getPainterHomePageURL(Document document, String cssQuery, String tagName, String attributeKey) {
        Elements elements = document.select(cssQuery).tagName(tagName);//   画师58434088的路径：/painter/14040
        List<String> strings = elements.stream().map(a -> a.attr(attributeKey)).collect(Collectors.toList());
        String painterUrl;
        if (strings.size() == 0) {
            throw new RuntimeException("很遗憾，未找到该ID对应的画师");
        } else {
            painterUrl = strings.get(0);
            System.out.println("已找到该画师，正在跳转至" + BasicUrl + painterUrl + "...");
        }
        System.out.println("正在跳转到该画师主页...");
        return painterUrl;
    }

    public List<String> getInfoList(Document document, String cssQuery, String tagName, String attributeKey) {
        Elements elements = document.select(cssQuery).tagName(tagName);
        // 利用stream获取图片链接
        List<String> stringList = elements.stream()
                .map(a -> a.attr(attributeKey))
                .collect(Collectors.toList());
        if (stringList.size() == 0) {
            throw new RuntimeException("图像获取数量为0，无法下载");
        }

        List<String> list = stringList.stream()
                .map(c -> c.substring(0, c.indexOf("?")))
                .collect(Collectors.toList());
        return list;
    }

}
